package com.yvan.periscope.service;

import com.yvan.es.RestFulResult;
import com.yvan.es.SearchAction;
import com.yvan.periscope.domain.HistogramBean;
import com.yvan.platform.JsonWapper;
import io.searchbox.client.JestClient;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * Created by Blues Zhao on 下午3:18 2018/8/16.
 */

@Service
@Slf4j
public class HistogramServiceImpl implements  HistogramService{

    @Autowired
    private JestClient jestClient;

    @Override
    public HistogramBean findHistogramFromPeriscope(String indexPattern, String histogramPrefix, long[] Partition,  DateTime dt1, DateTime dt2,String xAxisTitle,String yAxisTitle) throws IOException {
        SearchAction action = new SearchAction(log);
        action.index = indexPattern;
        action.size = 0;
        action.query.filter.range("@timestamp",dt1.toString(),false,dt2.toString(),true);
        action.agg.sum(histogramPrefix+"_0");
        action.agg.sum(histogramPrefix+"_1");
        action.agg.sum(histogramPrefix+"_2");
        action.agg.sum(histogramPrefix+"_3");
        action.agg.sum(histogramPrefix+"_4");
        action.agg.sum(histogramPrefix+"_5");
        action.agg.sum(histogramPrefix+"_6");
        action.agg.sum(histogramPrefix + "_m");


        RestFulResult r = action.search(jestClient);
        JsonWapper jw = r.getJsonWapper();
        Object data1 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_0@"+histogramPrefix+"_0")).get("value");
        Object data2 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_1@"+histogramPrefix+"_1")).get("value");
        Object data3 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_2@"+histogramPrefix+"_2")).get("value");
        Object data4 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_3@"+histogramPrefix+"_3")).get("value");
        Object data5 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_4@"+histogramPrefix+"_4")).get("value");
        Object data6 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_5@"+histogramPrefix+"_5")).get("value");
        Object data7 = ((LinkedHashMap)jw.get("aggregations","group_by@"+histogramPrefix+"_6@"+histogramPrefix+"_6")).get("value");
        Object dataM = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_m@" + histogramPrefix + "_m")).get("value");

        HistogramBean histogramBean = new HistogramBean();
        histogramBean.addData(data1);
        histogramBean.addData(data2);
        histogramBean.addData(data3);
        histogramBean.addData(data4);
        histogramBean.addData(data5);
        histogramBean.addData(data6);
        histogramBean.addData(data7);
        histogramBean.addData(dataM);

        histogramBean.addTitleArray(Partition);
        histogramBean.setxAxisTitle(xAxisTitle);
        histogramBean.setyAxisTitle(yAxisTitle);
        return histogramBean;
    }
}
